<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class jenisbarang extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_jenisbarang');
        $this->load->model('m_kategoribarang');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/jenisbarang/list.html");


        $search = $this->tsession->userdata('master_jenisbarang_search');
        $this->smarty->assign("search", $search);
        $jenisbarang_name = empty($search['jenisbarang_name']) ? '%' : '%' . $search['jenisbarang_name'] . '%';
        $kategoribarang_cd = empty($search['kategoribarang_cd']) ? '%' : $search['kategoribarang_cd'];

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/jenisbarang/index/");
        $config['total_rows'] = $this->m_jenisbarang->get_count_all_jenisbarang_search(array($jenisbarang_name, $kategoribarang_cd));
        $config['uri_segment'] = 4;
        $config['per_page'] = 100;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($jenisbarang_name, $kategoribarang_cd, ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_jenisbarang->get_all_jenisbarang_pagination_search($params));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_jenisbarang_search');
        } else {
            $params = array(
                "jenisbarang_name" => $this->input->post("jenisbarang_name"),
                "kategoribarang_cd" => $this->input->post("kategoribarang_cd")
            );
            $this->tsession->set_userdata("master_jenisbarang_search", $params);
        }
        // redirect
        redirect("master/jenisbarang");
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/jenisbarang/add.html");

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input 
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori Barang', 'trim|required');
        $this->tnotification->set_rules('jenisbarang_cd', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('jenisbarang_name', 'Nama', 'trim|required|max_length[100]');

        //set parameter
        $params = array(
            $this->input->post('jenisbarang_cd'),
            $this->input->post('jenisbarang_name'),
            $this->input->post('kategoribarang_cd'),
            $this->com_user['user_id']
        );

        //print_r($params);     
        //cek data
        //$jum_check_kode = $this->m_jenisbarang->get_count_all_jenisbarang_by_kode($params);
        $jum_check_kode_name = $this->m_jenisbarang->get_count_all_jenisbarang_by_jenisbarang_cd_kategoribarang_cd(array($this->input->post('jenisbarang_cd'), $this->input->post('kategoribarang_cd')));

        //echo $jum_check_kode_name;
        //exit();

        $jum_char_kode = strlen($this->input->post('jenisbarang_cd'));

        // process
        if ($jum_check_kode_name > 0) {
            $this->tnotification->sent_notification("error", "Kode sudah digunakan");
        } else if ($this->tnotification->run() !== FALSE) {
            // insert            
            if ($this->m_jenisbarang->insert($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jenisbarang/add");
    }

    public function edit($jenisbarang_cd, $kategoribarang_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/jenisbarang/edit.html");

        //data
        $result = $this->m_jenisbarang->get_jenisbarang_by_jenisbarang_cd_kategoribarang_cd(array($jenisbarang_cd, $kategoribarang_cd));
        $this->smarty->assign("result", $result);

        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('jenisbarang_cd', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('jenisbarang_name', 'Nama', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori', 'trim|required|max_length[100]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array(
                $this->input->post('jenisbarang_name'),
                $this->com_user['user_id'],
                $this->input->post('jenisbarang_cd'),
                $this->input->post('kategoribarang_cd')
            );
            // insert
            if ($this->m_jenisbarang->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jenisbarang/edit/" . $this->input->post('jenisbarang_cd').'/'.$this->input->post('kategoribarang_cd'));
    }

    public function delete($jenisbarang_cd, $kategoribarang_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/jenisbarang/delete.html");
        //data
        $result = $this->m_jenisbarang->get_jenisbarang_by_jenisbarang_cd_kategoribarang_cd(array($jenisbarang_cd, $kategoribarang_cd));
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('jenisbarang_cd', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('jenisbarang_cd'),$this->input->post('kategoribarang_cd'));
            // insert
            if ($this->m_jenisbarang->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/jenisbarang/");
    }

}
